//========encoding:utf-8=========
/*
 * @describe:  三目运算符
 * @author:mhao
 * @copyright:chinasoft.cn
 * @date:2020年3月18日   下午8:12:45
 */
package edu.chinasoft.handle;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import edu.chinasoft.pojo.User;
@Controller("handler04")
@RequestMapping("/handler04")
public class Handler04 {
	private Logger logger = LoggerFactory.getLogger(Handler04.class);
	@RequestMapping(value="/method01.do",method= {RequestMethod.POST,RequestMethod.GET})
	public ModelAndView method01(@RequestHeader(value = "User-Agent") String  userAgent, @RequestHeader("Referer") String referer,String user_name,String user_password,ModelAndView modelAndView) throws IOException {
		// TODO Auto-generated method stub
		logger.info("handler04>>>method01 start ……");
		//获取请求头里的数据
		logger.info(userAgent);
		logger.info(referer);
		//请求转发
		modelAndView.setViewName("/loginsuccess03");
		logger.info("handler04>>>method01 end ……");
		return modelAndView;
	}
	@RequestMapping(value="/method02.do",method = {RequestMethod.POST,RequestMethod.GET})
	public ModelAndView method02(@CookieValue(value = "JSESSIONID") String sessionId, String user_name, String user_password, ModelAndView modelAndView) throws IOException {
		// TODO Auto-generated method stub
		logger.info("handler04>>>method02 start ……");
		//获取cookie里的内容
		logger.info("sessionID为："+sessionId+"");
		User user = new User(user_name,user_password);
		//把服务层返回的结果保存到request域里
		modelAndView.addObject("user", user);
		//请求转发
		modelAndView.setViewName("/loginsuccess03");
		logger.info("handler04>>>method02 end ……");
		return modelAndView;
	}
	 @RequestMapping(value = "/method03.do",method = {RequestMethod.POST,RequestMethod.GET})
	    public String  method03(String user_name, String user_password, ModelMap model) throws IOException {
	        logger.info("Handler04>>>method03 start ……");
	        User user = new User(user_name,user_password);
	        //把服务层返回的结果保存到request域里
	        model.addAttribute("user", user);
	        //实现页面跳转   请求重发
	        String result="redirect:/jsp/loginsuccess03.jsp";
	        logger.info("Handler04>>>method03 end ……");
			return result;
	 }
	@RequestMapping(value = "/method04.do",method = {RequestMethod.POST,RequestMethod.GET})
    public String  method04(SessionStatus sessionStatus) throws IOException {
        logger.info("Handler04>>>method04 start ……");
        sessionStatus.setComplete();
		//实现页面跳转(请求重发所要跳转的页面)
        
		String result="redirect:/jsp/loginsuccess03.jsp";
		logger.info("handler04>>>method04 end ……");
		return result;
	}
    @RequestMapping(value = "/method05.do",method = {RequestMethod.POST,RequestMethod.GET})
    public String  method05(@ModelAttribute(value="user") User user,ModelMap modelMap) throws IOException {
        logger.info("Handler04>>>method05 start ……");
		//把服务层返回的结果保存到request域里
		modelMap.addAttribute("user", user);
		//实现页面跳转(请求重发)
		String result="forward:/jsp/loginsuccess02.jsp";
		logger.info("handler04>>>method04 end ……");
		return result;
	}
    @ModelAttribute()
    public void init(ModelMap modelMap) {
    	logger.info("Handler04>>>init start ……");
    	User result = new User();
    	result.setAge(23);
    	modelMap.addAttribute("user", result);
    	logger.info("Handler04>>>init end ……");
	}
}
